package cn.itacst.upload;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class UploadServlet2 extends HttpServlet {

	/**
	 		fileItem.getName());  // file字段为文档的名字, 普通字段因为没有名字,所以为null
			fileItem.getString("UTF-8");  // file字段为文档中的 "内容" , 普通字段为 value值
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1）	将DiskFileItemFactory对象绑定到ServletFIleUpload对象
		DiskFileItemFactory factory = new DiskFileItemFactory();
		//      创建ServletFIleUpload对象
		ServletFileUpload fileUpload = new ServletFileUpload(factory);
		//2）	ServletFIleUpload对象解析request对象
		try {
			//3）	Request对象中的数据，最终会被解析成一个一个的FileItem对象。
			List<FileItem> list = fileUpload.parseRequest(request);
			//遍历集合，拿出数据
			for (FileItem fileItem : list) {
				//判断当前的fileItem（input输入框）是不是一个简单文本字段
				//true 说明是一个简单文本字段   false说明是一个文件上传的字段
				if(fileItem.isFormField()){
					String fieldName = fileItem.getFieldName(); // 获取字段的名字
					System.out.println("普通字段fileItem.getFieldName="+fieldName);
					String value = fileItem.getString("UTF-8"); // 获取字段的值
					System.out.println("普通字段fileItem.getString(\"UTF-8\")="+value);//value输入的内容
					System.out.println("普通字段fileItem.getName()="+fileItem.getName()); // null
					System.out.println();
				}else{
					//不是普通字段的情况
					//确认向硬盘写出的路径，如何获取upload文件的路径
					String realPath = getServletContext().getRealPath("/uploadFolder");

					//获取文件名
					String fileName = fileItem.getName();
					System.out.println("realPath===="+realPath);
					//获取的是文档的名字,对于普通字段名字为null
					System.out.println("file字段fileItem.getName()="+fileItem.getName());
					// 获取的是文档中的内容
					String value = fileItem.getString("UTF-8");
					System.out.println("file字段fileItem.getString(\"UTF-8\"="+value);
					System.out.println();
					File file = new File(realPath,fileName);
					//向服务器硬盘写出数据
					try {
						fileItem.write(file);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		response.setContentType("text/html;charset=utf-8");
		String content = "你好,我来自UploadServlet2";

		String prefix="<font size='4' color='red'>";
		String postfix="</font>";
		String responseContent=prefix+content+postfix;
		response.getWriter().print(responseContent);

		System.out.println("----------------------------UploadServlet2 结束------------------------------");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");

		System.out.println("desc is "+request.getParameter("desc"));
		doGet(request, response);
	}

}
